package com.weacar.adapter;

import android.content.Context;
import android.os.CountDownTimer;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.weacar.R;
import com.weacar.bean.CarBean;
import com.weacar.utils.TimeUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TimeCountDownAdapter extends BaseAdapter {

    private List<CarBean.MsgBean> mDatas;
    private Context mContext;

    private LayoutInflater mInflater;

    //用于退出 Activity,避免 Countdown，造成资源浪费。
    private SparseArray<CountDownTimer> countDownCounters;


    public TimeCountDownAdapter(List<CarBean.MsgBean> mDatas, Context mContext) {
        this.mDatas = mDatas;
        this.mContext = mContext;
        this.countDownCounters = new SparseArray<>();
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int i) {
        return mDatas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        CarBean.MsgBean timeBean = mDatas.get(i);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_lv_search_result, viewGroup, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        // 是否显示一口价
        if (timeBean.getAuction_type() == 2) {
            holder.tv_kill_price.setVisibility(View.VISIBLE);
        } else {
            holder.tv_kill_price.setVisibility(View.GONE);
        }

        // 倒计时相关
        if (timeBean.getEnd_time() != null && !timeBean.getEnd_time().equals("")) {
            holder.tv_time.setVisibility(View.VISIBLE);
            CountDownTimer countDownTimer = countDownCounters.get(holder.tv_time.hashCode());
            if (countDownTimer != null) {
                //将复用的倒计时清除
                countDownTimer.cancel();
            }
            final ViewHolder finalHolder = holder;
            long timer = 0;
            timer = TimeUtil.dateDiff(timeBean.getEnd_time());
            if (timer > 0) {
                countDownTimer = new CountDownTimer(timer, 1000) {
                    @Override
                    public void onTick(long l) {
                        long time = l / 1000;
                        finalHolder.tv_time.setText(TimeUtil.formatLongToTimerStr(time));
                    }

                    @Override
                    public void onFinish() {
                        finalHolder.tv_time.setText("时间结束");
                    }
                }.start();
                countDownCounters.put(holder.tv_time.hashCode(), countDownTimer);
            } else {
                finalHolder.tv_time.setText("时间结束");
            }
        } else {
            holder.tv_time.setVisibility(View.GONE);
        }


        holder.tv_title.setText(timeBean.getName());
        holder.tv_params.setText(timeBean.getLicensing_date() + "|" + timeBean.getCapacity_name() + "|" + String.format("%.2f", timeBean.getDisplayed_mileage()) + "万公里");
        long price = Long.parseLong(timeBean.getOnline_price().substring(0, timeBean.getOnline_price().indexOf(".")));
        if (timeBean.getAuction_type() == 2) {
            holder.tv_price.setText(String.format("%.2f", (double) price / 10000) + "万秒杀");
            holder.tv_price.setVisibility(View.VISIBLE);
        } else if (timeBean.getAuction_type() == 1) {
            holder.tv_price.setVisibility(View.INVISIBLE);
            holder.tv_price.setText("100元起");
        }

        Glide.with(mContext).load(timeBean.getCar_img()).into(holder.iv_car);


        return convertView;
    }

    static class ViewHolder {

        @BindView(R.id.tv_time)
        TextView tv_time;
        @BindView(R.id.tv_params)
        TextView tv_params;
        @BindView(R.id.iv_car)
        ImageView iv_car;
        @BindView(R.id.tv_title)
        TextView tv_title;
        @BindView(R.id.tv_price)
        TextView tv_price;
        @BindView(R.id.tv_price_count)
        TextView tv_price_count;
        @BindView(R.id.tv_kill_price)
        TextView tv_kill_price;


        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


    public void cancelAllTimers() {
        if (countDownCounters == null) {
            return;
        }
        for (int i = 0, length = countDownCounters.size(); i < length; i++) {
            CountDownTimer cdt = countDownCounters.get(countDownCounters.keyAt(i));
            if (cdt != null) {
                cdt.cancel();
            }
        }
    }
}
